<?php
/**
 * @package html
 */
require_once '_dbobject.php';
require_once '_localemanager.php';
require_once '_img.php';
require_once '_system.php';
require_once '_stringex.php';
require_once '_htmlbase.php';

require_once '_htmlofferadmin.php';
require_once '_htmlofferlist.php';
require_once '_htmlofferthumbnail.php';

/**
 * Class which encapsulates view of one offer.
 */
class HtmlOffer extends HtmlOfferBase {
	/**
	 * @see HtmlBase::fetchInstance
	 * @return HtmlOffer
	 */
	static function fetchInstance(resource $resource) {
		try {
			return new HtmlOffer($resource);
		} catch(ExceptionResourceEmpty $e) {
			return false;
		}
	}
	
	/**
	 * Beginning of html code.
	 */
	const HTML_BEGIN = '
			<div class="offer">
	            <dl class="framed">
	            	<dt style="height: 13px;">
						<span class="left"><img src="/img/3dotTop.png" alt="dot" /> _TEMPLATE_TITLE_</span>
						<span class="right"><img src="/img/3dotTop.png" alt="dot" /></span>
						<span class="cleaner"></span>
					</dt>
	            </dl>
            	<div class="wrapper">
					<div id="mainImgWrapper" class="framed">
						<div class="swc0"><div class="swc1">
								<div class="swc2">
									<img id="mainImg" src="_TEMPLATE_MAINIMGSRC_" alt="" />
								</div><span class="swc3" />
						</div></div>
					</div>
					<div id="textWrapper">
	                    <dl class="framed">
	                    	<dt>
	                    		<img class="pixDown" src="/img/3dotLeft.png" alt="dot" /> _TEMPLATECODE_DESCRIPTION_:
	                    	</dt>
							<dd>
		                        _TEMPLATE_TEXT_
							</dd>
	                    </dl>
	                    <dl class="framed">
	                    	<dt><img class="pixDown" src="/img/3dotLeft.png" alt="dot" /> _TEMPLATECODE_PRICE_: </dt>
							<dd>
	                        	_TEMPLATE_PRICE_
							</dd>
	                    </dl>
	                    <dl class="framed">
	                    	<dt><img class="pixDown" src="/img/3dotLeft.png" alt="dot" /> _TEMPLATECODE_CONTACT_: </dt>
							<dd style="text-align: right; min-height: 60px;"><div class="left" style="text-align: left">
	                        	_TEMPLATE_FULLNAME_
	                        	</div>
	                        	<img src="_TEMPLATE_AGENTIMG_" alt=""/>
							</dd>
	                    </dl>
                	</div>
                	<div style="width: 520px;" class="float: left">
                	';
	
	/**
	 * Html code of one thumbnail.
	 */
	const HTML_THUMBNAIL = '
						<div class="thumbwrapped framed"> 
							<div class="swc0"><div class="swc1">
								<div class="swc2"> 
									<a href="#" onclick="javascript:Common.show(\'_TEMPLATE_FULLPICFULLNAME_\'); return false;" >
										<img src="_TEMPLATE_THUMBSRC_" alt="" />
									</a>
								</div>
								<span class="swc3"/>
							</div></div>
						</div>';
	
	/**
	 * Ending of html code.
	 */
	const HTML_END = '
						<div class="cleaner"></div>
					</div>
					<div class="cleaner"></div>
				</div>
			</div>';
	
	/**
	 * @see HtmlObject::processMe
	 */
	public function processMe() {
		$info = SiteContext::getSiteContext();
		$dbObject = $this->dbObject;
		$cm = CodeManager::getCodeManager();
		// Unrepeated html code.
		$this->buffer = self::HTML_BEGIN;
		
		// Get maintainer for this offer.
		$res = $info->getDBConnector()->query("SELECT * FROM users WHERE id = $dbObject->agentId LIMIT 0 , 1");

		$user = User::mysqlFetch($res);
		if ($user == null) {
			throw new ExceptionIllegalState("Offer $dbObject->id has no agent ($dbObject->agentId) assigned.");
		}
		
		$this->processImages(self::HTML_THUMBNAIL);
		
		$this->buffer .= self::HTML_END;
		
		// Code substitution.
		$this->replaceTemplate("/_TEMPLATE_TITLE_/", $dbObject->resolveCodeShortcut($dbObject->title));
		$this->replaceTemplate("/_TEMPLATE_TEXT_/", $dbObject->resolveCodeShortcut($dbObject->text));
		$this->replaceTemplate("/_TEMPLATE_PRICE_/", LocaleManager::price($dbObject->price, $dbObject->currency));
		$contact = "$user->firstName $user->surName<br/>".$cm->get("30006").": $user->mail<br/>"
			.$cm->get("30028"). ": $user->phone";
		$this->replaceTemplate("/_TEMPLATE_FULLNAME_/", $contact);
		if ($fileName = $user->fetchImgFilename()) {
			$webPath = $info->getSiteInf()->getPathWebImg() . Constants::FS_DIRDELIMITER . $user->imgSubdir();
			$thumbFileName = $info->getSiteInf()->imgFullPicThumbViewName($fileName);
			$fileName = $user->resizePictureIfThereIsNotAlreadyOne($fileName, Policy::imageFullPicThumbViewPrefix(), $thumbFileName, Constants::IMG_FULLPICTHUMBVIEWMAXSIZE);
			$this->replaceTemplate("/_TEMPLATE_AGENTIMG_/", $webPath.Constants::FS_DIRDELIMITER.$thumbFileName);
		}
		
		// Locale substitution.
		$cm = CodeManager::getCodeManager($info);
		$this->replaceTemplate("/_TEMPLATECODE_DESCRIPTION_/", $cm->get("30011"));
		$this->replaceTemplate("/_TEMPLATECODE_PRICE_/", $cm->get("30010"));
		$this->replaceTemplate("/_TEMPLATECODE_CONTACT_/", $cm->get("30012"));
		
	}
	
	/**
	 * @see HtmlObject::init
	 */
	public static function init(string $id = null) {
		$logger = Logger::getLogger();
		$info = SiteContext::getSiteContext();
		$cm = CodeManager::getCodeManager();
		
		if ($id == null) {
			@$id = (int)$_REQUEST[Constants::REQ_TYPE];
		} 

		$res = $info->getDBConnector()->query(Constants::DB_QUERY_OFFERS_ALL." WHERE id = $id AND status = 'visible' LIMIT 0 , 1");
		
		while($htmlOffer = HtmlOffer::fetchInstance($res)) {
			Logger::getLogger()->debug("processing offer: " . $htmlOffer->dbObject->id);
			try {
				$htmlOffer->processMe();
				$htmlOffer->flushMe();
			} catch (Exception $e) {
				$logger->error("Cannot display offer: '".$htmlOffer->dbObject->id."' due to error: '$e'");
				Error::getError()->addErrorMessage($cm->get("30205", array($htmlOffer->dbObject->id)));
			}
		}
	}
}
?>